package com.pshcp.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.SysDictDTO;
import com.pshcp.domain.model.SysDict;
import com.pshcp.mapper.SysDictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-22
 */
@Service
public class SysDictService {

    @Autowired
    private SysDictMapper sysDictMapper;

    public PageList<SysDict> pageList(SysDictDTO sysDictDTO){
        if(sysDictDTO.getPageNum() != null && sysDictDTO.getPageSize() != null){
            PageHelper.startPage(sysDictDTO.getPageNum(), sysDictDTO.getPageSize());
        }
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        if(sysDictDTO.getStatus() != null){
            criteria.andEqualTo("status", sysDictDTO.getStatus());
        }else{
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        example.setOrderByClause(" created_time desc");
        List<SysDict> list = sysDictMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<SysDict> list(SysDictDTO sysDictDTO){
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        if(sysDictDTO.getStatus() != null){
            criteria.andEqualTo("status", sysDictDTO.getStatus());
        }else{
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        if(sysDictDTO.getType() != null){
            criteria.andEqualTo("type", sysDictDTO.getType());
        }
        if(!ObjectUtils.isEmpty(sysDictDTO.getParentCode())){
            criteria.andEqualTo("parentCode", sysDictDTO.getParentCode());
        }
        if(sysDictDTO.getType() != null && sysDictDTO.getType().equals("address")){
            example.setOrderByClause(" code");
        }else{
            example.setOrderByClause(" sort");
        }
        return sysDictMapper.selectByExample(example);
    }

    public JSONArray findDictByType(SysDictDTO sysDictDTO){
        JSONArray jsonArray = new JSONArray();
        List<SysDict> sysDicts = list(sysDictDTO);
        if(CollectionUtils.isEmpty(sysDicts)){
            return jsonArray;
        }
        sysDicts.forEach(d -> {
            JSONObject obj = new JSONObject();
            obj.put("code", d.getCode());
            obj.put("name", d.getName());
            jsonArray.add(obj);
        });
        return jsonArray;
    }

    public List<SysDict> select(SysDict sysDict){
        return sysDictMapper.select(sysDict);
    }

    public int insert(SysDict sysDict){
        sysDict.setId(SystemUtil.getId());
        sysDict.setStatus(CommonStatusEnum.INIT.getCode());
        sysDict.setCreatedTime(new Date());
        return sysDictMapper.insert(sysDict);
    }

    public int update(SysDict sysDict){
        sysDict.setUpdateTime(new Date());
        return sysDictMapper.updateByPrimaryKeySelective(sysDict);
    }

    public SysDict findById(Long id){
        return sysDictMapper.selectByPrimaryKey(id);
    }

    public List<SysDict> findByIds(List<Long> ids){
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return sysDictMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        SysDict sysDict = new SysDict();
        sysDict.setStatus(CommonStatusEnum.DEL.getCode());
        return sysDictMapper.updateByExampleSelective(sysDict, example);
    }

}
